package org.csu.mypetstore.web.servlet;


import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.service.AccountService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;


@WebServlet(name = "ViewAccountServlet", value = "/ViewAccountServlet")
public class ViewAccountServlet extends HttpServlet {
    private static String SIGN_ON_ACCOUNT="/WEB-INF/jsp/catalog/Main.jsp";
    private static final String SIGN_ON="/WEB-INF/jsp/account/SignonForm.jsp";
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        AccountService accountService=new AccountService();
        HttpSession session=request.getSession();
        String userid=request.getParameter("username");
        String password=request.getParameter("password");

        //增加验证码
        String clientCheckcode = request.getParameter("checkcode");
        String serverCheckcode = (String) request.getSession().getAttribute("checkcode");

        Account account=new Account();
        account.setPassword(password);
        account.setUsername(userid);
        //System.out.println(password+"hhhh");
        //System.out.println(account.getUsername()+"hhhh");
        Account account1= new Account();



        try {
            account1 = accountService.getAccountByUsernameAndPassword(account);
            //System.out.println(account1.getPhone());
        } catch (Exception e) {
            e.printStackTrace();
        }
        //验证码验证功能
        if (clientCheckcode.equals(serverCheckcode)) {//将客户端验证码和服务器端验证比较，如果相等，则表示验证通过
            System.out.println("验证码验证通过！");
            if (account1 == null) {
                request.setAttribute("msg", "用户名或密码错误");
                request.getRequestDispatcher(SIGN_ON).forward(request, response);
            } else {

                if ("OK".equals(account1.getStatus())) {
                    request.setAttribute("msg", "您已登录");
                    request.getRequestDispatcher(SIGN_ON).forward(request, response);
                } else {
                    session.setAttribute("accounts", account1);
                    //System.out.println(account1.getUsername()+"hhhh");
                    //System.out.println(((Account)session.getAttribute("accounts")).getUsername());
                    account1.setStatus("OK");
                    String s=account1.getUsername();
                    accountService.updateAccount(account1,s);
                    System.out.println(account1.getStatus() + "hhhhh");
                    request.getRequestDispatcher(SIGN_ON_ACCOUNT).forward(request, response);
                }
            }
        }
        else {
            System.out.println("验证码验证失败！");
            session.setAttribute("msg","验证码错误");
            request.getRequestDispatcher(SIGN_ON).forward(request,response);
        }





    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
